
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" import="com.uft.model.entity.*,java.util.List"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ page isELIgnored="false"%>
 <script>
  $(document).ready(function () {
	 
		$( "#adminMenu" ).tabs({ heightStyle: "fill" });
		$( "#profilePreferenceTab" ).tabs({ heightStyle: "auto" });
		$("#profilePreferenceForm").jsForm();		
        $('#ProfilesTableContainer').jtable({
            title: 'Profiles',
            selecting: true,
            selectingCheckboxes: true,
            multiselect: false,
            actions: {
                listAction: 'getProfileList.htm',
                createAction: 'addProfile.htm',
                updateAction: 'updateProfile.htm',
                deleteAction: 'deleteProfile.htm'
            },
            toolbar: {
            	items: [
                {
                    icon: 'images/16/go-bottom-5.png',
                    text: 'Update Servers',
                    click: function () {
                        //perform your custom job...
                    	var $selectedRows = $('#ProfilesTableContainer').jtable('selectedRows');
                        $selectedRows.each(function () {
                        	 var record = $(this).data('record');
                        	 var profileId = record.profileId;
                        	 $.ajax({
                              	  type: "POST",
                              	  dataType: 'json',
                              	  url: "updateServerLists.htm",
                                  global: false,
                              	  data: {
                              		profileId: profileId  
                              	  },
                              	  success: function(data){
                              		  if (data.Result == "OK") {
	                              			if(data.Message =="scheduled"){
	                              				uftDeploy.submitNotificationTask(data.UniqueId);
	                              				 generateDiag("center","success", "Update Scheduled");
	                              			}else{
	                              				generateDiag("center","info",data.Message );
	                              			}
                              			  
                              			 
                              		  }else{
                              			  generateDiag("center","error", data.Message);
                              		  }
                              	  },
                              	  error: function () {
                              		  generateDiag("center","error", "Error Connecting Server");
                              	  },
                              	beforeSend: function(){
                    				waitingDialog({title: "Update Servers", message: "Updating Server List..."});
                    			},
                    			complete: function(){
                    				closeWaitingDialog();
                    			}
                              	  
                              	});
                        });
                    }
                },
                {
                    icon: 'images/16/network-connect-2.png',
                    text: 'Test Connection',
                    click: function () {
                        //perform your custom job...
                    	var $selectedRows = $('#ProfilesTableContainer').jtable('selectedRows');
                        $selectedRows.each(function () {
                        	 var record = $(this).data('record');
                        	 var profileId = record.profileId;
                        	 $.ajax({
                           	  type: "POST",
                           	  dataType: 'json',
                           	 global: false,
                           	  url: "testProfileConnection.htm",
                           	  data: {
                           		profileId: profileId  
                           	  },
                           	  success: function(result){
                           		  if (result.Result == "OK") {
                           			  generateDiag("center","success", "Connection Tested. OK!");
                           		  }else{
                           			  generateDiag("center","error", result.Message);
                           		  }
                           	  },
                           	  error: function () {
                           		  generateDiag("center","error", "Error Connecting Server");
                           	  },
	                           	beforeSend: function(){
	                				waitingDialog({title: "Test Connection", message: "Testing Connections..."});
	                			},
	                			complete: function(){
	                				closeWaitingDialog();
	                			}
                           	  
                           	});
                        });
                    }
                }
                ]
            },
            fields: {
                profileId: {  key: true,list: false },
                region: { title: 'Region'},
                profileName: { title: 'Profile Name' },
                hostname: { title: 'Hostname' },
                port: { title: 'Port'},
                security: { title: 'Security',
                    type: 'checkbox',
                    values: { 'false': 'false', 'true': 'true' },
                    defaultValue: 'false' },
                username: { title: 'Username' },
                 password: {
                    title: 'Password',
                    type: 'password',
                    list: false
                }
            },
            selectionChanged: function () {
            	var $selectedRows = $('#ProfilesTableContainer').jtable('selectedRows');
            	$selectedRows.each(function () {
               	 var record = $(this).data('record');
               	 var profilePref = record.profilePref;
                $("#profilePreferenceForm").jsForm("fill",profilePref );
               	
               	
               });
            }
        });
              
     /*   $('#PreferencesTableContainer').jtable({
        	title: 'Preferences',
            selecting: true,
            selectingCheckboxes: true,
            actions: {
                listAction: 'getPreferencesList.htm',
                createAction: 'addPreference.htm',
                updateAction: 'updatePreference.htm',
                deleteAction: 'deletePreference.htm'
            },
            fields: {
                preferenceId: { key: true,list: false },
                prefName: { title: 'Name'},
                prefValue: { title: 'Value'},
                prefCategory: { title: 'Category'}
             }
        }); */
        $('#UsersTableContainer').jtable({
        	title: 'Users and Groups',
            selecting: true,
            selectingCheckboxes: true,
            actions: {
                listAction: 'getUserList.htm',
                createAction: 'addUser.htm',
                updateAction: 'updateUser.htm',
                deleteAction: 'deleteUser.htm'
            },
            fields: {
            	userid: { key: true,list: false },
                username: { title: 'Username'},
                password: {
                    title: 'Password',
                    type: 'password',
                    list: false
                },
                enabled: { title: 'Active',
                    type: 'checkbox',
                    values: { 'false': 'false', 'true': 'true' },
                    defaultValue: 'false' },
                groups: { title: 'Groups'},
                admin: { title: 'Administrator',
                    type: 'checkbox',
                    values: { 'false': 'false', 'true': 'true' },
                    defaultValue: 'false' }
                
             }
        }); 
        $('#ProfilesTableContainer').jtable('load');
       /* $('#PreferencesTableContainer').jtable('load'); */
        $('#UsersTableContainer').jtable('load');
        $('#preferenceSaveBtn').button({
        	icons: {
                primary: "ui-icon-disk"
              }
        }).click(function( event ) {
            var profilePreference = $("#profilePreferenceForm").jsForm("get");
            $.ajax({
            	  type: "POST",
            	  dataType: 'json',
            	  url: "updateProfilePreference.htm",
            	  data: profilePreference,
            	  success: function(result){
 
            		  if (result.Result == "OK") {
            			  generateDiag("center","success", "Success!");
            		  }else{
            			  generateDiag("center","error", result.Message);
            		  }
            	  },
            	  error: function () {
            		  generateDiag("center","error", "Error Connecting Server");
            	  }
            	  
            	});
        });
        $('#testConnectionBtn').button({
        	
        }).click(function(event){
        	var $selectedRows = $('#ProfilesTableContainer').jtable('selectedRows');
            $selectedRows.each(function () {
            	 var record = $(this).data('record');
            	 var profileId = record.profileId;
            	 var username=$('#sshUsername').val();
            	 var password=$('#sshPassword').val();
            	 $.ajax({
                  	  type: "POST",
                  	  dataType: 'json',
                  	  global: false,
                  	  url: "testSSHConnection.htm",
                  	  data: {
                  		profileId: profileId, 
                  		username: username,
                  		password: password
                  	  },
                  	  success: function(result){
                  		  if (result.Result == "OK") {
                  			  generateDiag("center","success", "Connection Tested. OK!");
                  		  }else{
                  			  generateDiag("center","error", result.Message);
                  		  }
                  	  },
                  	  error: function () {
                  		  generateDiag("center","error", "Error Connecting Server");
                  	  },
                      	beforeSend: function(){
           				waitingDialog({title: "Test Connection", message: "Testing Connections..."});
           			},
           			complete: function(){
           				closeWaitingDialog();
           			}
                  	  
                  	});
            });
        })
        
       
    });
  </script>
	<div id="adminMenu" style="border:none">
	  <ul>
	    <li><a href="#profilesTab">Profiles</a></li>
	    
	    <!-- <li><a href="#Preferences">Preferences</a></li> -->
	    <li><a href="<c:url value="getPreferencesView.htm"/>">Preferences</a></li>
	    <li><a href="#users">Users/Groups</a></li>
	     <li><a href="<c:url value="/jsp/audit.jsp"/>">Audit</a></li>
	    <li><a href="<c:url value="/jsp/runtimeDisplay.jsp"/>">Runtime</a></li>
	   
	  </ul>
	  <div id="profilesTab">
	    <h2>Profiles</h2>
	    <div id="ProfilesTableContainer" height="50%"></div>
	       <table width="100%"><tr>
	       <td><h3><img src="images/16/applications-system-3.png">Profile Preferences</h3></td>
	       <td align="right">
	       	<button id="preferenceSaveBtn">Save</button>
	       </td></tr></table>
		   <div id="profilePreferenceForm">
				<input type="hidden" id="prefId" name="data.prefId"/>
		    	<div id="profilePreferenceTab">
				  <ul>
				    <li><a href="#generialInfo">General</a></li>
				    <li><a href="#sshInfo">SSH Configuration</a></li>
				    <li><a href="#scriptDeploy">Script Deploy</a></li>
				    <li><a href="#scriptConfig">Script Config</a></li>
				   <li><a href="#security">Security</a></li>
				  </ul>
				  <div id="generialInfo">
				   	OS: 
				   	<select name="data.osName" id="osName" class="select ui-widget-content ui-corner-all">
			                <option value="AIX">AIX</option>
			                <option value="WINDOWS">WINDOWS</option>
			                <option value="LINUX">LINUX</option>                
			        </select>
			        <br>
			        Scripts Path: <input type="text" name="data.scriptPath" id="scriptPath" class="text ui-widget-content ui-corner-all" /> 
				  </div>
				  <div id="sshInfo">
				  	SSH Enabled<input type="checkbox" name="data.sshEnabled"  id="sshEnabled" class="checkbox ui-widget-content ui-corner-all"/>
				  	SSH Username: <input type="text" name="data.sshUsername" id="sshUsername" class="text ui-widget-content ui-corner-all" /> 
				   	SSH Password: <input type="password" name="data.sshPassword" id="sshPassword" class="password ui-widget-content ui-corner-all" />
				   	<button id="testConnectionBtn">Test Connection</button>
				  </div>
				   <div id="scriptDeploy">
				   	Script: <input type="text" name="data.deployScript" id="deployScript" class="text ui-widget-content ui-corner-all" /> 
				   	Script Sudo:<input type="text" name="data.deployScriptSudo"  id="deployScriptSudo" class="text ui-widget-content ui-corner-all" /> 
				   	Script Working Dir:<input type="text" name="data.deployScriptWorkingdir" id="deployScriptWorkingdir" class="text ui-widget-content ui-corner-all" />
				    Script Env:<input type="text" name="data.deployEnv" id="deployEnv" class="text ui-widget-content ui-corner-all" />  
				  </div>
				 <div id="scriptConfig">
			  		 	Configuration Script: <input type="text" name="data.configScript" class="text ui-widget-content ui-corner-all" /> 
			    </div>  
				  <div id="security">
				  	SOAP Config File: <input type="text" name="data.soapConfig" id="soapConfig" class="text ui-widget-content ui-corner-all" /> 
				  	SSL Config File: <input type="text" name="data.sslConfig" id="sslConfig" class="text ui-widget-content ui-corner-all" />
				  	Trust Store: <input type="text" name="data.trustStore" id="trustStore" class="text ui-widget-content ui-corner-all" /> 
				  	Trust Password: <input type="password" name="data.trustPassword" id="trustPassword" class="text ui-widget-content ui-corner-all" />
				  	Key Store: <input type="text" name="data.keyStore" id="keyStore" class="text ui-widget-content ui-corner-all" /> 
				  	Key Password: <input type="password" name="data.keyPassword" id="keyPassword" class="text ui-widget-content ui-corner-all" />
				  </div>
				 </div>
		   </div>
	  </div>
	  <div id="users">
	    <h2>Users and Groups </h2>
	    <div id="UsersTableContainer"></div>
	  </div>
	</div>
